﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Kenly.DBFramework.Gateway
{   
    /// <summary>
    /// Represents a attribute that contains metadata to describe the association between container and property.
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public sealed class OneToManyAssociationAttribute : Attribute
    {
        private Type _elementType;
        private string _resultOrder = "";
        private string _resultFilter;

        /// <summary>
        /// 
        /// </summary>
        public OneToManyAssociationAttribute()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        public static AssociationType AssociationType
        {
            get
            {
                return AssociationType.OneToMany;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public Type ElementType
        {
            get
            {
                return _elementType;
            }
            set
            {
                _elementType = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public string ResultFilter
        {
            get
            {
                return _resultFilter;
            }
            set
            {
                _resultFilter = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public string ResultOrder
        {
            get
            {
                return _resultOrder;
            }
            set
            {
                _resultOrder = value;
            }
        }

        /// <summary>
        /// Converts to an Association.
        /// </summary>
        /// <param name="oneToManyAssociation"></param>
        /// <returns></returns>
        public static implicit operator AssociationAttribute(OneToManyAssociationAttribute oneToManyAssociation)
        {
            return ToAssociationAttribute(oneToManyAssociation);
        }

        /// <summary>
        /// Converts to an Association.
        /// </summary>
        /// <param name="oneToManyAssociation"></param>
        /// <returns></returns>
        public static AssociationAttribute ToAssociationAttribute(OneToManyAssociationAttribute oneToManyAssociation)
        {
            AssociationAttribute relationAttribute = new AssociationAttribute(AssociationType.OneToMany);
            if (oneToManyAssociation == null)
            {
                return relationAttribute;
            }
            relationAttribute.ElementType = oneToManyAssociation.ElementType;
            relationAttribute.ResultFilter=oneToManyAssociation.ResultFilter;
            relationAttribute.ResultOrder=oneToManyAssociation.ResultOrder;
            return relationAttribute;
        }

    }
}
